
#include "StdAfx.h"
#include "LobbyService.h"
#include "RoomManager.h"
#include <tinyxml.h>
#include <LogService.h>
#include <StringObj.h>
#include "NotifyManager.h"
#include "AllUser_RoomExit_Default.h"

#ifdef WIN32
#pragma comment( lib, "tinyxml.lib" )
#endif

LobbyService::LobbyService(){	
	users_ = 0;
	rooms_ = 0;
	channel_ = 0;

	lobby_enter_ = 0;
	room_create_ = 0;
	room_enter_ = 0;
	room_exit_ = 0;
	room_to_room_ = 0;

	max_room_ = 0;
	room_mode_ = 0;
	
	room_max_user_ = DEF_ROOM_MAX_USER;
	room_max_observer_user_ = DEF_ROOM_MAX_OBSERVER_USER;
	lobby_max_user_ = DEF_LOBBY_MAX_USER;
	
	operater_ = new OperateProcess();
	notify_ = new NotifyManager();
}

LobbyService::~LobbyService(){
	SAFE_DELETE( operater_ );
}

LobbyService* LobbyService::Instance(){
	if(! LobbyService_ )
		LobbyService_ = new LobbyService();
	
	return LobbyService_;
}

void LobbyService::Shutdown(){
	SAFE_DELETE( LobbyService_ );
}

bool LobbyService::initialize( const char* file_name ){
	if(! loadEnvironment( file_name ) )
		return false;

	/*
	if(! notify_->load( (char*)file_name, getTick() ) )
		return false;
		*/

	rooms_->initialize( max_room_ );
	return true;
}

void LobbyService::setMgrInstance( UserManager* users, RoomManager* rooms, \
		   LobbyManager* channel )
{
	users_ = users;
	rooms_ = rooms;
	channel_ = channel;	

	LOG_INSTANCE( LOG_SERVICE( "instance" )->Log("board mgr inst set users : %u", users_ ) );
	LOG_INSTANCE( LOG_SERVICE( "instance" )->Log("board mgr inst set rooms : %u", rooms_ ) );
	LOG_INSTANCE( LOG_SERVICE( "instance" )->Log("board mgr inst set lobby : %u", channel_ ) );
}

void LobbyService::setActionInstance( LobbyEnter* lobby_enter, \
			RoomCreate* room_create, RoomEnter* room_enter,RoomExit* room_exit, \
			RoomToRoom* roomToRoom )
{
	lobby_enter_ = lobby_enter;
	room_create_ = room_create;
	room_enter_ = room_enter;
	room_exit_ = room_exit;
	room_to_room_ = roomToRoom;

	LOG_INSTANCE( LOG_SERVICE( "instance" )->Log("board action inst set lobby enter : %u", lobby_enter_ ) );
	LOG_INSTANCE( LOG_SERVICE( "instance" )->Log("board action inst set room create : %u", room_create_ ) );
	LOG_INSTANCE( LOG_SERVICE( "instance" )->Log("board action inst set room enter : %u", room_enter_ ) );
	LOG_INSTANCE( LOG_SERVICE( "instance" )->Log("board action inst set room exit : %u", room_exit_ ) );
	LOG_INSTANCE( LOG_SERVICE( "instance" )->Log("board action inst set room to room : %u", room_to_room_ ) );
}

int LobbyService::arrange_lobby_instance(){
	int result = 0;

	if( LobbyService::Instance()->isRoomModeNormal() )
		result = rooms_->process_delete_rooms();

	return result;
}

bool LobbyService::loadEnvironment( const char* file_name ){
	LOG_INSTANCE( LOG_SERVICE( "instance" )->Log("* board initialize value : file name = %s", file_name ) );

	TiXmlDocument doc( file_name );

	if( doc.LoadFile() == false ){
		int line = doc.ErrorRow();
		int col = doc.ErrorCol();

		LOG_ERR( LOG_SERVICE( "instance" )->Log \
			("error load db session info xml = line : %u, col : %u", line, col) );

		return false;
	}

	if( doc.Error() )
		return false;
	
	TiXmlNode* node = 0;
	TiXmlAttribute* attr = 0;
	TiXmlElement* element = doc.RootElement();

	StringObj value;
	value = (char*)element->Value();
	if(! value.isEqualIgnLetterType("config") )
		return false;

	node = element->FirstChild("board");
	node->ToElement();
	value = node->FirstChild("max_user")->FirstChild()->Value();
	lobby_max_user_ = value.to4ByteNumber();
	IS_NULL_RETURN( lobby_max_user_, false );
	
	value = node->FirstChild("max_room_user")->FirstChild()->Value();
	room_max_user_ = value.to4ByteNumber();
	IS_NULL_RETURN( room_max_user_, false );
	
	TiXmlNode* observer_node = 0;
	observer_node = node->FirstChild("max_room_observer_user");
	if( observer_node ){
		value = observer_node->FirstChild()->Value();
		room_max_observer_user_ = value.to4ByteNumber();
	}	

	value = node->FirstChild("max_room")->FirstChild()->Value();
	max_room_ = value.to4ByteNumber();
	IS_NULL_RETURN( max_room_, false );

	value = node->FirstChild("room_mode")->FirstChild()->Value();
	room_mode_ = value.to4ByteNumber();

	return true;
}
